package com.example.qihuibao.widget.dialog

import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.qihuibao.R
import com.example.qihuibao.base.BaseDialogFragment
import com.example.qihuibao.bean.MealSet
import com.example.qihuibao.bean.MealSetFee
import com.example.qihuibao.bean.RspQueryFeesBean
import com.example.qihuibao.bean.RspQueryMealInfoBean
import com.example.qihuibao.constant.Config
import com.example.qihuibao.dao.MyDatabase
import com.example.qihuibao.utils.extension.toast
import com.example.qihuibao.widget.view.CommonRecyclerView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 *@author: cww
 *@desc: 现场取餐（就餐）
 *@date: 5/16/23 10:58 AM
 */
class SceneMealDialog(val click:(ok:Boolean, mealset: MealSet?, fee: MealSetFee?)->Unit) : BaseDialogFragment(R.layout.dialog_scene_meal) {
    private var rv_mealset:CommonRecyclerView? = null
    private var rv_fee:CommonRecyclerView? = null
    private var mealsetIndex = -1
    private var mealsetFeeIndex = -1
    private var meal:RspQueryMealInfoBean? = null
    private var fee:RspQueryFeesBean? = null

    override fun initView() {
        rv_mealset = mView?.findViewById<CommonRecyclerView>(R.id.rv_mealset)
        rv_fee = mView?.findViewById<CommonRecyclerView>(R.id.rv_fee)
        mView?.findViewById<Button>(R.id.btn_cancel)?.setOnClickListener {
            dismiss()
            click(false, null, null)
        }
        mView?.findViewById<Button>(R.id.btn_ok)?.setOnClickListener {
            if (mealsetIndex == -1) {
                activity?.toast("请选择一个套餐")
                return@setOnClickListener
            }
            if (mealsetFeeIndex == -1) {
                activity?.toast("请选择一个收费标准")
                return@setOnClickListener
            }
            dismiss()
            click(true, meal?.MealSetlList!![mealsetIndex], fee?.MealSetFeeList!![mealsetFeeIndex])
        }
        lifecycleScope.launch {

            withContext(Dispatchers.IO) {
                meal = MyDatabase.db.mealInfoDao().queryInId(Config.currentCanteenId, Config.currentMealId)
                fee = MyDatabase.db.feesInfoDao().queryInId(Config.currentCanteenId, Config.currentMealId)
            }
            if (!meal?.MealSetlList.isNullOrEmpty()) {
                rv_mealset?.init(
                    R.layout.item_mealset2,
                    LinearLayoutManager(context, RecyclerView.HORIZONTAL, false),
                    meal?.MealSetlList,
                    {parent, pos ->
                        parent.findViewById<TextView>(R.id.tv_mealset_name).text = getString(R.string.mealset_name, meal?.MealSetlList!![pos].MealSetName)
                        parent.findViewById<TextView>(R.id.tv_mealset_status).text = getString(R.string.mealset_status, if (meal?.MealSetlList!![pos].Status=="0") "不开通" else "开通")
                        parent.findViewById<TextView>(R.id.tv_mealset_PerOrderLimit).text = getString(R.string.mealset_PerOrderLimit, meal?.MealSetlList!![pos].PerOrderLimit)
                        parent.findViewById<TextView>(R.id.tv_mealset_PerSpotLimit).text = getString(R.string.mealset_PerSpotLimit, meal?.MealSetlList!![pos].PerSpotLimit)
                        parent.findViewById<TextView>(R.id.tv_mealset_PerTotalLimit).text = getString(R.string.mealset_PerTotalLimit, meal?.MealSetlList!![pos].PerTotalLimit)
                        parent.findViewById<TextView>(R.id.tv_mealset_AllOrderLimit).text = getString(R.string.mealset_AllOrderLimit, meal?.MealSetlList!![pos].AllOrderLimit)
                        parent.findViewById<TextView>(R.id.tv_mealset_AllSpotLimit).text = getString(R.string.mealset_AllSpotLimit, meal?.MealSetlList!![pos].AllSpotLimit)
                        parent.findViewById<TextView>(R.id.tv_mealset_AllTotalLimit).text = getString(R.string.mealset_AllTotalLimit, meal?.MealSetlList!![pos].AllTotalLimit)
                        parent.findViewById<ImageView>(R.id.iv_select).visibility = if (mealsetIndex == pos) View.VISIBLE else View.GONE
                    },
                    {
                        mealsetIndex = it
                        rv_mealset?.adapter?.notifyDataSetChanged()
                    }
                )
                //mealset fee
                if (!fee?.MealSetFeeList.isNullOrEmpty()) {
                    rv_fee?.init(
                        R.layout.item_mealset_fee2,
                        LinearLayoutManager(context, RecyclerView.HORIZONTAL, false),
                        fee?.MealSetFeeList,
                        {parent, pos ->
                            parent.findViewById<TextView>(R.id.tv_mealset_fee).text =
                                "套餐ID:${fee?.MealSetFeeList!![pos].MealSetID}\n" +
                                    "订餐第一份价格:${fee?.MealSetFeeList!![pos].OrderFee}\n" +
                                    "订餐第二份价格:${fee?.MealSetFeeList!![pos].OrderFee2}\n" +
                                    "订餐第三份后价格:${fee?.MealSetFeeList!![pos].OrderFee3}\n" +
                                    "现场第一份价格:${fee?.MealSetFeeList!![pos].SpotFee}\n" +
                                    "现场第二份价格:${fee?.MealSetFeeList!![pos].SpotFee2}\n" +
                                    "现场第三份后价格:${fee?.MealSetFeeList!![pos].SpotFee3}"
                            parent.findViewById<ImageView>(R.id.iv_select).visibility = if (mealsetFeeIndex == pos) View.VISIBLE else View.GONE
                        },
                        {
                            mealsetFeeIndex = it
                            rv_fee?.adapter?.notifyDataSetChanged()
                        }
                    )
                }
            }
        }
    }

}